<?php
/**
 * (jobs) 回写备货设定日期(单个操作)
 */
namespace App\Jobs\Stocking;

use App\Models\Stocking\StockingSku;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
class SetDate implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $areaId;
    protected $classifyId;
    protected $setDate;

    /**
     * SetDate constructor.
     * @param $areaId
     * @param $classifyId
     * @param $setDate
     */
    public function __construct($areaId,$classifyId,$setDate)
    {
        $this->areaId = $areaId;
        $this->classifyId = $classifyId;
        $this->setDate = $setDate;
    }

    /**
     * Execute the job.
     * @return void
     */
    public function handle()
    {
        $this->exeFillData($this->areaId,$this->classifyId,$this->setDate);
    }

    private function exeFillData($areaId,$classifyId,$setDate){
        $ss = StockingSku::query()
            ->where('area_id',$areaId)
            ->whereHas('sku',function ($query)use ($classifyId){
                $query->where('classify_id',$classifyId);
            })->get();
        foreach ($ss as $_ss){
            $_ss->set_date = $setDate; //备货时间
            $_ss->save();

            $_ss->over_stock = StockingSku::getOverStock($_ss); //积压库存
            $_ss->over_stock_value = StockingSku::getOverStockValue($_ss->over_stock,$_ss->unit_price); //积压货值
            $_ss->save();
        }
    }
}
